package com.luoqiu.springCloud.controller;

import com.luoqiu.springCloud.service.OrderService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 洛秋y
 * @date 2020/11/20 15:12
 */
@RestController
@DefaultProperties(defaultFallback = "globalHandler")
public class OrderController {

    @Resource
    private OrderService orderService;

    @HystrixCommand()//此方法开启服务降级  采用全局的备用方法
    @GetMapping(value = "/consumer/hys/ok")
    public String ok() {
        return orderService.ok();
    }

    @HystrixCommand(fallbackMethod =  "failHandler", commandProperties = {
            //超过1.5秒 就报异常 走备用方法
            @HystrixProperty(name = "execution.isolation.thread.timeoutMilliseconds", value = "1500")
    })
    @GetMapping(value = "/consumer/hys/err")
    public String err() {
        return orderService.err();
    }

    public String failHandler() {
        return "有点忙";
    }

    //下面是全局fallback
    public String globalHandler() {
        return "全局服务降级";
    }
}
